package faceb.server.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Repository;

import com.google.gson.Gson;

import faceb.server.dao.CourseDao;
import faceb.server.model.Course;
import faceb.server.util.MyBatisUtil;

@Repository("courseDao")
public class CourseDaoImpl implements CourseDao {

	private SqlSessionFactory factory = MyBatisUtil.getsqlSessionFactory();

	private Gson gson = new Gson();

	public Map<String, String> getCoursesByUserId(String userid) {
		SqlSession session = factory.openSession();
		List<Course> courses = session.selectList(
				"CourseMapper.getCoursesByUserId", userid);
		Map<String, String> contents = new HashMap<String, String>();
		for (int i = 0; i < courses.size(); i++) {
			Course c = courses.get(i);
			contents.put("_" + c.getC_term_count() , c.getC_content());
			contents.put("_" + c.getC_term_count() +"_courseid", c.getC_id());
		}
		session.commit();
		session.close();

		return contents;
	}

	public int insertCourse(Course c) {
		SqlSession session = factory.openSession();
		int result = session.insert("CourseMapper.insertCourse", c);

		session.commit();
		session.close();
		return result;
	}

	public int updateCourse(Course c) {
		SqlSession session = factory.openSession();
		int result = session.insert("CourseMapper.updateCourse", c);

		session.commit();
		session.close();
		return result;
	}
	
	
	public static void main(String[] args) {
		Course c = new Course("666", 2, "1", "cccccccccccccccccc");
		int result = new CourseDaoImpl().updateCourse(c);
		System.out.println(result );
	}

}
